package com.readaloud.mongo;

import java.util.List;
import java.util.Map;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.readaloud.hibernate.user.Books;
import com.readaloud.indexing.KeyWord;
import com.readaloud.indexing.KeyWordMongo;


public class InvertedIndexMongo {
	public static void main(String[] args) {
		InvertedIndexMongo invertedIndexMongo = new InvertedIndexMongo();
		//invertedIndexMongo.saveKeywordMongo();
	}

	public void saveKeywordMongo(List<Books> bookList) {

		try {
			Mongo mongo = new Mongo();// "localhost", 64609);
			Morphia morph = new Morphia();
			morph.map(KeyWordMongo.class);
			Datastore datastore = morph.createDatastore(mongo, "test");

			System.out.println("MongoDB connection created.");

			DB testDb = mongo.getDB("test");
			DBCollection dbc = testDb.getCollection("KeyWordMongo");
			
			for (Books book : bookList) {
				
				book.getKeywords();

				String word = "";
				KeyWord keyword;
				for (Map.Entry<String, KeyWord> entry : book.getKeywords()
						.entrySet()) {
					word = entry.getKey();
					keyword = entry.getValue();
					KeyWordMongo keyWordMongo = new KeyWordMongo();
					keyWordMongo.setBookId(book.getBookId());
					keyWordMongo.setFrequency(keyword.getFrequency());
					keyWordMongo.setPos(keyword.getPosition());
					keyWordMongo.setStdev(keyword.getStandardDeviation());
					keyWordMongo.setWord(keyword.getWord());
					datastore.save(keyWordMongo);
				}

				/*
				 * keyWordMongo.setBookId(1); keyWordMongo.setWord("rocky");
				 * keyWordMongo.getPos().add(1); keyWordMongo.getPos().add(2);
				 * keyWordMongo.getPos().add(3);
				 */
				// datastore.save(keyWordMongo);
				System.out.println("Data Saved.");
			}
			/*List<KeyWordMongo> keyWordMongoList = datastore
					.find(KeyWordMongo.class).filter("bookId", 1)
					.filter("word", "rocky").asList();
			
			 * List<KeyWordMongo> keyWordMongoList =
			 * datastore.find(KeyWordMongo.class).filter("bookId", 1).asList();
			 

			for (KeyWordMongo te : keyWordMongoList) {
				System.out.println(te.getBookId());
				System.out.println(te.getWord());
				System.out.println(te.getPos());
				// System.out.println(i+"-->"+tt.getId()+":"+tt.getName()+":"+tt.getPos());
			}*/
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}